﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickORM.BLL;
using QuickORM.Entity;
using QuickORM.Common;
public partial class Manage_WebPages_UserInfo_UserManager : System.Web.UI.Page
{
    public string Mode
    {
        get { return ViewState["mode"] == null ? "add" : ViewState["mode"].ToString(); }
        set { ViewState["mode"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Mode = Request.QueryString["action"];
            string id = Request.QueryString["id"] as string;
            BindDDL(ddlRole);
            if (!string.IsNullOrEmpty(id))
            {
                T_UserInfo userInfo = new T_UserInfo();
                userInfo = BLLFactory<T_UserInfoManager>.Instance.FindByID(id);
                BindPage(userInfo);
                ViewState["ID"] = id;
            }
        }
    }
    #region Fill DroDownList
    protected void BindDDL(DropDownList ddl)
    {
        ddl.DataSource = BLLFactory<T_RoleManager>.Instance.GetAll();
        ddl.DataBind();
    }
    #endregion
    #region 填充页面
    /// <summary>
    /// 将数据填充到页面显示
    /// </summary>
    /// <param name="info">实体类数据</param>
    protected void BindPage(T_UserInfo info)
    {
        txtUserName.Value = info.UserName;
        if(Mode=="edit")
        {
            txtUserPWD.Disabled = true;
        }
      //  txtUserPWD.Value = "123456";
        txtRealName.Value = info.RealName;
        txtDep.Value = info.Dep;
        txtEmail.Value = info.Email;
        txtTel.Value = info.Tel;
        ddlRole.SelectedValue = info.RoleID.ToString();
    }
    #endregion
    #region 提交数据
    protected void btn_Click(object sender, EventArgs eventArgs)
    {
        string js = "alert('操作失败！');";
        T_UserInfo user = new T_UserInfo();
        user.UserName = Util.querySQL(txtUserName.Value.Trim());
        if (!string.IsNullOrEmpty(txtUserPWD.Value))
        {
            user.UserPWD = Util.SHA1(txtUserPWD.Value);
        }
        
        user.RealName = Util.querySQL(txtRealName.Value.Trim());
        user.Dep = Util.querySQL(txtDep.Value.Trim());
        user.Email = Util.querySQL(txtEmail.Value.Trim());
        user.Tel = Util.querySQL(txtTel.Value.Trim());
        user.Status = 1;
        user.RoleID = int.Parse(ddlRole.SelectedValue);
        user.CreatTime = DateTime.Now;
        if (Mode == "add")
        {
            //新增
            if (BLLFactory<T_UserInfoManager>.Instance.Insert(user))
            {
                js = "window.opener.select();window.close();";
            }

        }
        else
        {
            //修改
            if (BLLFactory<T_UserInfoManager>.Instance.Edit(user, ViewState["ID"].ToString())>0)
            {
                js = "window.opener.select();window.close();";
            }
        }
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", js, true);
    }
    #endregion
}